#pragma once
#include "__threadPool.hpp"
namespace xyl
{
    // START OF CLASS ThreadPool
    template <typename T>
    class ThreadPool
    {
    public:
        static ThreadPool<T> *get_instance()
        {
            if(nullptr == _instance)
            {
                lock_guard lg(&_instance_mtx);
                if(nullptr == _instance)
                {
                    _instance = new ThreadPool<T>();
                    _instance->start();
                }
            }
            return _instance;
        }

        void push_task(const T &task) { _tp.__push_task(task); }

    private:
        void start() { _tp.__start(); }

        ThreadPool(size_t threadNum = DEFAULT_THREAD_NUM)
            : _tp(threadNum)
        {
            std::cout << "ThreadPool::ThreadPool()\n";
        }
        ThreadPool(const ThreadPool<T> &) = delete;
        void operator=(const ThreadPool<T> &) = delete;

    private:
        __ThreadPool<T> _tp;
        static ThreadPool<T> *_instance;       // 单例对象
        static pthread_mutex_t _instance_mtx; // 单例对象的锁
    };
    // END OF CLASS ThreadPool

    template <typename T>
    ThreadPool<T> *ThreadPool<T>::_instance = nullptr;
    template <typename T>
    pthread_mutex_t ThreadPool<T>::_instance_mtx = PTHREAD_MUTEX_INITIALIZER;
}